package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianjipms.dao.RoleMapper;
import com.tianjipms.dao.UserMapper;
import com.tianjipms.entity.Bill;
import com.tianjipms.entity.House;
import com.tianjipms.entity.User;
import com.tianjipms.service.BillService;
import com.tianjipms.service.HouseService;
import com.tianjipms.utils.Result;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.WeakHashMap;


@RestController
@RequestMapping("/api/utilities")
public class UtilitiesController {
    @Resource
    private BillService billService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private HouseService houseService;
    //定期收取物业费
    @Scheduled(cron = "0 31 0 * * *")
    public Result backupData() {
        List<User> list = userMapper.findUserIdListByRoleId(4L);
        for(int i = 0; i < list.size(); i++){
            Bill bill = new Bill();
            bill.setUserId(list.get(i).getId());
            bill.setPayeeId(1L);
            bill.setMessage("物业费费用");
            bill.setCreateTime(new Date());
            Long id = list.get(i).getId();
            QueryWrapper<House> queryWrapper = new QueryWrapper<House>();
            queryWrapper.eq("user_id", list.get(i).getId());
            List<House> houseList = houseService.list(queryWrapper);

            if(houseList.size() == 1){
                BigDecimal temp = BigDecimal.valueOf(Integer.parseInt(houseList.get(0).getArea())).multiply(BigDecimal.valueOf(18));
                bill.setPrice(temp);
                bill.setStatus(0);
                billService.save(bill);
            }else {
                for(int j = 0; j < list.size(); j++){
                    BigDecimal temp = BigDecimal.valueOf(Integer.parseInt(houseList.get(j).getArea())).multiply(BigDecimal.valueOf(18));
                    bill.setPrice(temp);
                    bill.setStatus(0);
                    billService.save(bill);
                }
            }

        }
        return Result.ok();
    }
}
